//
//  ProgramXMLParser.m
//  Mehfile Zainab
//
//  Created by Sitara Badourali on 31/07/12.
//  Copyright (c) 2012 Mehfile Zainab. All rights reserved.
//

#import "ProgramXMLParser.h"
#import "MZProgram.h"

@implementation ProgramXMLParser

@synthesize program;

- (ProgramXMLParser *) initXMLParser:(MZProgramViewController*)aViewController {
    
    [super init];
    
    viewController = aViewController;
    
    currentElementValue = [[NSMutableString alloc]init];
    
    return self;
}


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {
    
    if([elementName isEqualToString:@"programs"]) {

        viewController.programArray = [[NSMutableArray alloc] init];
        
    }
    
    if([elementName isEqualToString:@"program"]) {
        
        program = [[MZProgram alloc] init];
        
    }

    if([elementName isEqualToString:@"schedules"]) {
        
        scheduleCount = 0;
        
    }
    
    if([elementName isEqualToString:@"schedule"]) {
        
        scheduleCount++;
        
    }
    
    //NSLog(@"Processing Element: %@", elementName);
    
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    
    [currentElementValue appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
    
}



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
    if([elementName isEqualToString:@"programs"])
        return;
    
    if([elementName isEqualToString:@"program"]) {
        [program release];
        program = nil;
    }

    if([elementName isEqualToString:@"schedule"]) {
        
        MZProgram *copyProgram = [[[MZProgram alloc] init] autorelease];
        copyProgram.mzProgramDate = program.mzProgramDate;
        copyProgram.mzProgramDescription = program.mzProgramDescription;
        copyProgram.mzProgramStartTime = program.mzProgramStartTime;
        copyProgram.mzProgramEndTime = program.mzProgramEndTime;
        copyProgram.mzProgramTitle = program.mzProgramTitle;
        
        [viewController.programArray addObject:copyProgram];
    }
    
    if([elementName isEqualToString:@"schedules"]) {
        scheduleCount = 0;
    }
    
    if([elementName isEqualToString:@"date"]) {
        program.mzProgramDate = currentElementValue;
    }
    
    if(([elementName isEqualToString:@"title"]) && (scheduleCount>0)) {
        program.mzProgramTitle = currentElementValue;
    }
    
    if([elementName isEqualToString:@"description"]) {
        program.mzProgramDescription = currentElementValue;
    }

    if([elementName isEqualToString:@"start_time"]) {
        program.mzProgramStartTime = currentElementValue;
    }

    if([elementName isEqualToString:@"end_time"]) {
        program.mzProgramEndTime = currentElementValue;
    }
    
    [currentElementValue release];
    currentElementValue = [[NSMutableString alloc]init];
    
}


- (void) dealloc {
    
    [program release];
    [currentElementValue release];
    [super dealloc];
}

@end